package com.org.qrtrace.controller;


import cn.hutool.http.HttpResponse;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.org.qrtrace.pojo.Ptype;
import com.org.qrtrace.service.ICheckhistoryService;
import com.org.qrtrace.service.IPtypeService;
import com.org.qrtrace.utils.CommonUtils;
import com.org.qrtrace.utils.JsonResult;
import com.org.qrtrace.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.models.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bonnie
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/type")
@Api(tags = "类型管理api")
public class PtypeController {

    @Autowired
    private IPtypeService service;



    @GetMapping("/lists")
    public JsonResult<?> getList(){
        return JsonResult.successInfo(service.list());
    }

    @PostMapping("/add")
    public JsonResult<?> addType(Ptype type, HttpServletRequest request) {
        try {
            CommonUtils.setTimeAndUser(type,request);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
        type.setTuid(CommonUtils.getUUID());
        boolean saved;
        try {
            saved = service.save(type);
        }catch (Exception exception){
            return JsonResult.fail("该类型已存在！");
        }

        if(saved){
            return JsonResult.success();
        }else {
            return JsonResult.fail("添加失败！");
        }
    }

    @PostMapping("delete")
    public JsonResult<?> removeType(Ptype type){
        boolean check = service.removeById(type);
        if(check){
            return JsonResult.success();
        }else {
            return JsonResult.fail("删除失败！");
        }
    }

    @PostMapping("update")
    public JsonResult<?> updateType(Ptype type){
        boolean check = service.updateById(type);
        if(check){
            return JsonResult.success();
        }else {
            return JsonResult.fail("更新失败！");
        }
    }
    @PostMapping("getOne")
    public JsonResult<?> getOneType(Ptype type){
        QueryWrapper<Ptype> wrapper = new QueryWrapper<>();
        String typeName = type.getType();
        wrapper.like("type",typeName);
        List<Ptype> types = service.list(wrapper);
        if(types != null && types.size()>0){
            return JsonResult.successInfo(types);
        }else {
            return JsonResult.fail("未查询到相关数据！");
        }
    }
}
